package mumu.android.monitoring;

import com.nd.pad.sdk.usercenter.db.dao.KeyValueTableDao;

import mumu.android.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

public class BatteylevelActivity extends Activity {
	/** Called when the activity is first created. */
	private TextView batterLevel;

	@Override
	/**
	 * Called when the current activity is first created.
	 */
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		// setContentView(R.layout.main);
		batterLevel = new TextView(this);
		// batterLevel = (TextView) this.findViewById(R.id.batteryLevel);
		batteryLevel();
		setContentView(batterLevel);
		startService(new Intent(this, AppService.class)); 
	}

	private void batteryLevel() {
		KeyValueTableDao.getInstance(this).put("asdf", System.currentTimeMillis() + "");
		BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
			public void onReceive(Context context, Intent intent) {
				context.unregisterReceiver(this);
				int rawlevel = intent.getIntExtra("level", -1);// 获得当前电量
				int scale = intent.getIntExtra("scale", -1);
				// 获得总电量
				int level = -1;
				if (rawlevel >= 0 && scale > 0) {
					level = (rawlevel * 100) / scale;
				}
				batterLevel.setText("Battery Level Remaining: " + level + "%");
			}
		};
		IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		registerReceiver(batteryLevelReceiver, batteryLevelFilter);
	}

}
